package org.holynite.fieldmanage.managed.backing

import org.holynite.fieldmanage.managed.model.ChargeRuleFormBean
import org.holynite.fieldmanage.managed.model.ChargeRulesBean
import org.holynite.fieldmanage.managed.model.ChargeSchemeFormBean
import org.holynite.fieldmanage.managed.model.ChargeSchemesBean

import javax.enterprise.context.RequestScoped
import javax.faces.bean.ManagedBean
import javax.faces.bean.ManagedProperty
import javax.faces.event.ValueChangeEvent

/**
 * Created by midnite on 15/2/1.
 */
@ManagedBean
@RequestScoped
class ChargeSchemeSelectBean implements Serializable {

    @ManagedProperty("#{chargeSchemeFormBean}")
    ChargeSchemeFormBean csForm
    @ManagedProperty("#{chargeRuleFormBean}")
    ChargeRuleFormBean crForm
    @ManagedProperty("#{chargeSchemesBean}")
    ChargeSchemesBean csBean
    @ManagedProperty("#{chargeRulesBean}")
    ChargeRulesBean crBean

    void loadSchemeDetail(ValueChangeEvent event) {
        def selected = event.newValue as String
        if (selected.equals("new_content")) {
            csForm.addMode = true
            csForm.clearForm()
            crForm.clearForm()
            crForm.clearSearch()
        } else {
            csForm.addMode = false
            def selectedScheme = csBean.findScheme(selected)
            csForm.schemeName = selectedScheme.name
            csForm.enableScheme = selectedScheme.enabled
            csForm.extendTime = selectedScheme.extendRuleTime
            csForm.preciseCharge = selectedScheme.preciseCharge
            crForm.clearForm()
            crBean.loadRulesInScheme(selected, crForm.searchKeyword)
        }
    }
}
